<script setup>
import { computed } from 'vue';

// props:['list']
const props = defineProps({
  list: {
    type: Array,
    default: () => []
  }
})

const emit = defineEmits(['changeDone', 'del', 'changeAll'])

// 计算全选状态
const isCheckAll = computed(() => {
  // 根据list每一项的状态计算出是否选中

  // every，遍历数组每一项，都符合条件，就返回true,对于空数组，也会返回true
  // some,值有有一个满足条件，就返回true
  return props.list.length > 0 && props.list.every(v => v.done)
})

// change事件，状态改变了，把改变后的状态传递到父组件
const changeAll = (e) => {
  // console.log(e.target.checked, '状态')
  emit('changeAll', e.target.checked)
}
</script>

<template>
  <section class="main">
    <input id="toggle-all" class="toggle-all" type="checkbox" :checked="isCheckAll" @change="changeAll" />
    <label for="toggle-all">Mark all as complete</label>
    <ul class="todo-list">
      <li :class="{ completed: item.done }" v-for="item in list" :key="item.id">
        <div class="view">
          <input class="toggle" type="checkbox" :checked="item.done" @change="emit('changeDone', item.id)" />
          <label>{{ item.name }}</label>
          <button class="destroy" @click="emit('del', item.id)"></button>
        </div>
        <input class="edit" value="Create a TodoMVC template" />
      </li>
    </ul>
  </section>
</template>

<style lang="less" scoped>
</style>
